<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    
    
	<table  id="tradesList"></table>

	<script>
	
	$("#tradesList").datagrid({
		title:"交易订单列表",
		singleSelect:false,
		collapsible:true,
		pagination:true,
		method:'get',
		url:'/trades/list',
		pageSize:50,
		pageList:[20,50,100,200],
		pagePosition:'top,bottom',
		sortName:'trade_time',
		sortOrder:'desc',
	    columns:[[    
	        {field:'tradeTime',title:'交易时间',halign:'center',width:150,sortable:true}, 
	        {field:'symbol',title:'交易对',halign:'center',width:70},  
	        {field:'tradeDirect',title:'方向',halign:'center',align:'center',width:70,formatter: function(value,row,index){
				return value == '1' ? '买入':'卖出';
			}},  
	        {field:'money',title:'金额',halign:'center',align:'right',width:160,formatter: function(value,row,index){
	        	if(row.tradeDirect == '1'){
	        		return value;
	        	}else{
	        		return row.amount;
	        	}
			}},
	        {field:'amount',title:'数量',halign:'center',align:'right',width:160,formatter: function(value,row,index){
	        	if(row.tradeDirect == '1'){
	        		return value;
	        	}else{
	        		return row.money;
	        	}
			}},
	        {field:'price',title:'单价',halign:'center',align:'right',width:130},
	        {field:'fee',title:'手续费【买入为币，卖出为钱】',halign:'center',align:'right',width:210,formatter: function(value,row,index){
	        	if(row.tradeDirect == '2'){
	        		return accounting.format(value,8,',');
	        	}else{
	        		return value;
	        	}
			}}
	    ]],
	    rowStyler: function(index,row){
			if (row.tradeDirect == '1'){
				return 'background-color:#33CC66';
			}else if(row.tradeDirect == '2'){
				
				return 'background-color:#E67E22';
			}
		},

		groupField:'groupId',
		view: groupview,
		groupFormatter:function(value, rows){
			//计算盈利百分比
			var sell = 0;
			var buy = 0;
			for(var i in rows){
				var item = rows[i];
				var direct = item.tradeDirect;
				if(direct == '1'){
					buy += (item.amount - item.fee)*item.price;
				}else{
					sell += (item.money * item.price) - item.fee;
				}
			}
			var title = rows[0].symbol + " - " + "第"+rows[0].loopCount+"轮";
			if(sell != 0 && buy != 0){
				var percent = (sell - buy) / buy * 100;
				percent = accounting.toFixed(percent,2);
				if(percent > 0 ){
					title += " - 收益率: " + "<span style='color:#0077BF'>" + percent + "% </span>";
				}else{
					title += " - 收益率: " + "<span style='color:red'>" + percent + "% </span>";
					
				}
			}
			return title;
			
			

		}
	});
	
	</script>